package com.module.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.module.dialog.inter.DialogOnClickListener;
import com.module.dialog.utils.ClickType;

/**
 * 弃用
 */
public abstract class BaseBottomDialog implements View.OnClickListener {

    private View parent = null;
    private TextView title = null;
    private FrameLayout container = null;
    private TextView btn1, btn2; //2个按钮
    private RelativeLayout titleLayout = null;

    protected Context mContext = null;
    private Dialog mDialog = null;
    private Window mWindow = null;
    private LayoutInflater inflaterFactory = null;

    private DialogOnClickListener onClickListener = null;

    public BaseBottomDialog(Context context) {
        this(context, 0);
    }

    public BaseBottomDialog(Context context, int windowStyleType) {
        this.mContext = context;
        init();
    }


    public void show(View v) {
        if (mContext instanceof Activity && ((Activity) mContext).isFinishing()) {
            return;
        }
        mDialog.show();
    }

    private void init() {
        inflaterFactory = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        initView();
        initDialog();
        initData();

    }

    private void initDialog() {
        mDialog = new Dialog(mContext, R.style.dialogStyle);
        mDialog.setContentView(parent);
        mDialog.setCanceledOnTouchOutside(false);
        mWindow = mDialog.getWindow();
        mWindow.setGravity(Gravity.BOTTOM);
        mWindow.setWindowAnimations(R.style.dialogAnimation);
        mWindow.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    private void initView() {
        int layoutId = R.layout.dialog_picker;
        parent = inflaterFactory.inflate(layoutId, null);
        titleLayout = parent.findViewById(R.id.layout_title);
        title = parent.findViewById(R.id.tv_title);
        btn1 = parent.findViewById(R.id.button1);
        btn2 = parent.findViewById(R.id.button2);
        container = parent.findViewById(R.id.contentPanel);
        //添加子布局
        inflaterFactory.inflate(getContentView(), container, true);
        initContentView(parent);

    }

    protected abstract int getContentView();

    protected abstract void initContentView(View parent);

    protected abstract void initData();

    public void dialogDismiss() {
        if (mDialog != null && mDialog.isShowing())
            mDialog.dismiss();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.button1) {
            if (onClickListener != null) {
                onClickListener.onClick(ClickType.Cancel);
            }
        }else if (v.getId() == R.id.button2) {
            if (onClickListener != null) {
                onClickListener.onClick(ClickType.Sure);
            }
        }
    }

    public void setCancelable(boolean bool) {
        mDialog.setCancelable(bool);
    }

    public void setCanceledOnTouchOutside(boolean cancel) {
        mDialog.setCanceledOnTouchOutside(cancel);
    }

    public void setTitle(CharSequence title) {
        if (!TextUtils.isEmpty(title)) {
            this.title.setText(title);
        }
    }

    public void showDialogTitle(boolean isVisibility) {
        titleLayout.setVisibility(isVisibility ? View.VISIBLE : View.GONE);
    }

    public void setOnClickListener(DialogOnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public void show() {
        show(mWindow.getDecorView());
    }

    public TextView getTitle() {
        return title;
    }

    public boolean isShowing() {
        return mDialog.isShowing();
    }


}